#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "Log.hpp"

class Ulit
{
public:
    static bool ReadFile(const std::string &path, std::string *fileCount)
    {
        // 1. 获取文件本身大小
        struct stat st;
        int ret = stat(path.c_str(), &st);
        if (ret < 0)
        {
            logMessage(Error, "stat error");
            return false;
        }
        // 2. 调整fileCount的大小
        fileCount->resize(st.st_size);
        // 3. 打开文件
        int fd = open(path.c_str(), O_RDONLY);
        if (fd < 0)
        {
            logMessage(Error, "open error");
            return false;
        }
        // 4. 读取文件
        ssize_t readSize = read(fd, (char*)fileCount->c_str() , st.st_size);
        if (readSize != st.st_size)
        {
            logMessage(Error, "read error");
            return false;
        }
        // 5. 关闭文件
        close(fd);

        logMessage(Info, "read file success, file size is %ld", readSize);
        return true;
    }
};